package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import cn.edu.zjgsu.ito.model.College;
import cn.edu.zjgsu.ito.service.AdminColManageService;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import com.jfinal.aop.Before;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;


@Before(AdminControllerInterceptor.class)
public class AdminColManageController extends Controller {

	AdminColManageService adminColManageService = ServiceFactory.createAdminColManageService();
	
	/**
	 * 学院管理初始化
	 * 黄凯航
	 */
	public void index()
	{
		List<Record> colleges=Db.find("select * from college");
		setAttr("college",colleges);
		render("collegemanage.html");
	}
	
	/**
	 * 添加学院
	 * 黄凯航
	 */
	public void submit()
	{
		College college = getModel(College.class,"college");
		String error=adminColManageService.addSingleCollege(college);
		setAttr("error",error);
		index();
	}
	
	/**
	 * 异步更新学院
	 * 黄凯航
	 */
	public void ajaxupdate()
	{
		String collegeCode=getPara("collegeCode");
		String collegeName=getPara("collegeName");
		int id=getParaToInt("id");
		
		College college = getModel(College.class);
		college.set("collegeCode", collegeCode);
		college.set("collegeName", collegeName);
		college.set("id", id);
		
		String error=adminColManageService.updateOrNot(college);
		setAttr("college",College.dao.find("select * from college where id=?",id));
		setAttr("error",error);
		renderJson();
	}
	
	/**
	 * 批量删除
	 * 黄凯航
	 */
	public void batch()
	{
		try{
			int id=0;
			String checkboxValue=getPara("checkboxValue");
			String[] colCheckboxs=checkboxValue.split(",");
			for(int i=0;i<colCheckboxs.length;i++)
				System.out.println(i+"="+colCheckboxs[i]+"   ");
			Record collegeCollegeCode=new Record();
			String collegeName="";
			for(int i=0;i<colCheckboxs.length;i++)
			{
				id=Integer.parseInt(colCheckboxs[i]);
				collegeCollegeCode=Db.findById("college", id);
				collegeName=collegeCollegeCode.get("collegeName");
				adminColManageService.deleteOrNot(collegeName);
				collegeCollegeCode=new Record();
			}
			redirect("/admincolmanage");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redirect("/admincolmanage");
		}
	}
	
	/**
	 * 学院模板下载
	 * 黄凯航
	 */
	public void fileDownload()
	{
		String fileName="学院模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;
		renderFile(new File(path));
	}
	
	/**
	 * 学院模板上传方法
	 * 黄凯航
	 */
	public void fileUpload()
	{
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try{
			UploadFile file = getFile("uploadFile");
			String fileName = file.getFileName();
			String extension = fileName.substring(fileName.lastIndexOf("."));
			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source = file.getFile();
				String newFileName = System.currentTimeMillis()+extension;
				FileInputStream fis = new FileInputStream(source);
				File targetDir = new File(filedir);
				File target = new File(targetDir,newFileName);
				FileOutputStream fos = new FileOutputStream(target);
				byte[] bts = new byte[300];
				while(fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				fos.close();
				fis.close();
				source.delete();
				String error = adminColManageService.readCollegeTemplate(fileName,newFileName,filedir);
				setAttr("error",error);
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
